import type { ButtonProps } from "@/components/ReDialog/type";

import { hasPerms } from "@/utils/auth";
import { isFunction } from "@pureadmin/utils";

/**
 * 根据权限生成弹窗底部按钮
 * @param perms 权限数组，如果用户有权限，则显示按钮，否则返回空数组
 * @param extraButtons 可选额外自定义按钮
 */
export function usePermissionFooterButtons(perms: string[], extraButtons: ButtonProps[] = []): ButtonProps[] {
  if (hasPerms(perms)) return [];

  const buttons: ButtonProps[] = [
    {
      label: "取消",
      text: true,
      bg: true,
      btnClick: ({ dialog: { options, index } }) => {
        const done = () => {
          if (options?.visible !== undefined) options.visible = false;
        };
        if (options?.beforeCancel && isFunction(options.beforeCancel)) {
          options.beforeCancel(done, { options, index });
        } else {
          done();
        }
      }
    }
  ];

  return buttons.concat(extraButtons);
}
